<template>
  <tl-sub-page :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="900px" @open="open" @close="close">
    <el-tabs v-model="activeTabName" type="card" @tab-click="tabClick">
      <el-tab-pane label="里程碑模板" name="basic">
        <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目等级" prop="projectlevel" :rules="rules.validateRequire">
                <el-select v-model="model.projectlevel" filterable clearable multiple>
                  <el-option v-for="item in codeModel.projectlevelList" :key="item.code" :label="item.name" :value="item.code" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="里程碑模板名称" prop="name" :rules="rules.validateRequire">
                <el-input v-model="model.name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="序号" prop="sortcode" :rules="rules.validateRequire">
                <el-input v-model="model.sortcode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="模板类型" prop="templatetype" :rules="rules.validateRequire">
                <el-select v-model="model.templatetype" filterable clearable>
                  <el-option v-for="item in codeModel.templatetypeList" :key="item.code" :label="item.name" :value="item.code" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="可交付成果" prop="deliverable" :rules="rules.validateRequire">
                <el-input v-model="model.deliverable" type="textarea" :rows="4"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="任务模板" name="line">
        <tl-line ref="line" :parentId="model.id" :projecttype="model.projecttype" :projectlevel="model.projectlevel" :mileposttemplate="model.name"></tl-line>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit"> <svg-icon icon-class="save" />保存 </el-button>
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_edit } from "@tapui/mixins/mixin_edit"
import { mixin_page_fields_custom } from '@/views/gpm/gpmProjectInfo/mixin/mixin_page_fields_custom.js'
/** 子组件 */
import tlLine from "../gpmProjectTaskTemplate/index"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
  components: { tlLine },
  mixins: [mixin_edit, mixin_page_fields_custom],
  data() {
    return {
      moduleTitle: "里程碑模板",
      urlPrefix: "/sd/milePostTemplate/",
      activeTabName: "basic",
      isRefreshLine: false,
      url: {
        // 额外的url
      }
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        this.$set(this.model, "projecttype", "0")
      }
    },
    open() {
      this.model = {}
      this.$nextTick(async () => {
        await this.getFieldsCustomSetting('sdMilePostTemplateEdit', 'el-form')
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = "添加" + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then(_ => {
              this.assignDefaultValue()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化，不初始化sortcode的值为0
            this.$set(this.model, "enabled", 1)
            this.assignDefaultValue()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = "编辑" + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        } else {
          this.title = "查看" + this.moduleTitle
          this.queryData(this.id).then(_ => {
            this.loaded()
          })
        }
      })
    },
    loaded() {
      // 页面加载完毕执行
      this.activeTabName = "basic"
      this.isRefreshLine = false
    },
    tabClick(tab) {
      if (tab.name === "line") {
        if (!this.model.id) {
          this.$nextTick(_ => {
            this.activeTabName = "basic"
          })
          this.$tap
            .confirm("当前数据未保存，是否立即保存数据？")
            .then(_ => {
              this.doSubmitEdit()
            })
            .catch(_ => {})
        } else if (!this.isRefreshLine) {
          this.$refs.line.doQuery()
          this.isRefreshLine = true
        }
        this.$refs.line.initPage()
      }
    },
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      this.$set(this.model, "billuid", this.uid)
      postAction(this.url.add, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message("添加成功", "success")
            this.model = res.result
            this.queryData(res.result.id)
            this.isAdd = false
            this.$emit("refresh")
          } else {
            this.$tap.alert(res.message, "warning")
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    doEdit() {
      this.saveLoading = true
      putAction(this.url.edit, this.model)
        .then(res => {
          if (res.success) {
            this.$tap.message(res.message, "success")
            this.isAdd = false
            this.queryData(this.model.id)
            this.$emit("refresh")
          } else {
            this.$tap.alert(res.message, "warning")
          }
          this.afterSubmit()
          this.saveLoading = false
        })
        .catch(err => {
          this.saveLoading = false
        })
    },
    /** 防重复提交查询唯一uuid */
    queryUid() {
      this.uid = ""
      if (!this.url.queryUid) {
        this.$tap.alert("请设置queryUid地址!", "warning")
        return
      }
      return getAction("api/uidGen/queryUid", {}).then(res => {
        if (res.success) {
          this.uid = res.result.billuid
        } else {
          this.$tap.alert(res.message, "warning")
        }
      })
    }
  }
}
</script>
